package com.contory.ssn_lgnd.app.maingame;

import android.util.Log;

import com.contory.ssn_lgnd.app.Assets;
import com.contory.ssn_lgnd.app.comp.ViewObject;
import com.contory.ssn_lgnd.framework.gl.SpriteBatcher;
import com.contory.ssn_lgnd.framework.gl.TextureRegion;
import com.contory.ssn_lgnd.framework.math.Polygon;
import com.contory.ssn_lgnd.framework.math.Vector2;

public class TerrainPartsTrianlge extends TerrainParts {
	int cellSize;
	ViewObject[] rectView;
	Vector2 viewPos;
	
	public static final int TOP_LEFT = 0;
	public static final int TOP_RIGHT = 1;
	public static final int DOWN_LEFT = 2;
	public static final int DOWN_RIGHT = 3;
	
	int type;
	public TerrainPartsTrianlge(float strX, float strY, int cellSize, int type){
		this.cellSize = cellSize;
		viewPos = new Vector2(strX - (TILE_WIDTH / 2) * (cellSize-1), strY - (TILE_HEIGHT / 2) * (cellSize-1));
		
		coliBound = new Polygon();
		coliBoundInit(type);
		int rectViewSize = (cellSize * cellSize + cellSize) / 2; 	
		rectView = new ViewObject[rectViewSize];
		for(int i = 0; i < rectView.length; i++)
			rectView[i] = new ViewObject();
		this.type = type;
		viewObjectInit(type);
	}

	private void coliBoundInit(int type){
		switch(type){
			case TOP_LEFT:
				coliBound.lockAddPoint(cellSize * TILE_WIDTH, cellSize * TILE_HEIGHT);
				coliBound.addPoint(0, 0);
				coliBound.addPoint(1, 1);
				coliBound.addPoint(0, 1);
				coliBound.unLockAddPoint();
				coliBound.setPos(viewPos.x, viewPos.y); 
			break;
			case TOP_RIGHT:
				coliBound.lockAddPoint(cellSize * TILE_WIDTH, cellSize * TILE_HEIGHT);
				coliBound.addPoint(1, 0);
				coliBound.addPoint(1, 1);
				coliBound.addPoint(0, 1);
				coliBound.unLockAddPoint();
				coliBound.setPos(viewPos.x, viewPos.y); 
			break;
			case DOWN_RIGHT:
				coliBound.lockAddPoint(cellSize * TILE_WIDTH, cellSize * TILE_HEIGHT);
				coliBound.addPoint(0, 0);
				coliBound.addPoint(1, 0);
				coliBound.addPoint(1, 1);
				coliBound.unLockAddPoint();
				coliBound.setPos(viewPos.x, viewPos.y); 
			break;
			case DOWN_LEFT:
				coliBound.lockAddPoint(cellSize * TILE_WIDTH, cellSize * TILE_HEIGHT);
				coliBound.addPoint(0, 0);
				coliBound.addPoint(1, 0);
				coliBound.addPoint(0, 1);
				coliBound.unLockAddPoint();
				coliBound.setPos(viewPos.x, viewPos.y); 
			break;
		}
	}
	
	private void viewObjectInit(int type){
		switch(type){
			case TOP_LEFT:
				float strX = viewPos.x - (float)(cellSize-1) * (TILE_WIDTH/2);
				float strY = viewPos.y + (float)(cellSize-1) * (TILE_HEIGHT/2);
				int idx = 0;
				for(int i = 0; i < cellSize; i++){
					for(int j = 0; j < cellSize-i; j++){
						TextureRegion tr;
						if(j == cellSize-i-1)
							tr = Assets.TEX_REG_TERRAIN_TRIANGLE_TOPLEFT;
						else
							tr = Assets.TEX_REG_TERRAIN_RECTTILE;
						
						rectView[idx++].setXY_WH_TR(strX + j*(TILE_WIDTH), 
								strY - i*(TILE_WIDTH), TILE_WIDTH, TILE_HEIGHT, tr);
					}
				}
			break;
			case TOP_RIGHT:
				strX = viewPos.x + (float)(cellSize-1) * (TILE_WIDTH/2);
				strY = viewPos.y + (float)(cellSize-1) * (TILE_HEIGHT/2);
				idx = 0;
				for(int i = 0; i < cellSize; i++){
					for(int j = 0; j < cellSize-i; j++){
						TextureRegion tr;
						if(j == cellSize-i-1)
							tr = Assets.TEX_REG_TERRAIN_TRIANGLE_TOPRIGHT;
						else
							tr = Assets.TEX_REG_TERRAIN_RECTTILE;
						
						rectView[idx++].setXY_WH_TR(strX - j*(TILE_WIDTH), 
								strY - i*(TILE_WIDTH), TILE_WIDTH, TILE_HEIGHT, tr);
					}
				}
			break;
			case DOWN_LEFT:
				strX = viewPos.x - (float)(cellSize-1) * (TILE_WIDTH/2);
				strY = viewPos.y - (float)(cellSize-1) * (TILE_HEIGHT/2);
				idx = 0;
				for(int i = 0; i < cellSize; i++){
					for(int j = 0; j < cellSize-i; j++){
						TextureRegion tr;
						if(j == cellSize-i-1)
							tr = Assets.TEX_REG_TERRAIN_TRIANGLE_DOWNLEFT;
						else
							tr = Assets.TEX_REG_TERRAIN_RECTTILE;
						
						rectView[idx++].setXY_WH_TR(strX + j*(TILE_WIDTH), 
								strY + i*(TILE_WIDTH), TILE_WIDTH, TILE_HEIGHT, tr);
					}
				}
			break;
			case DOWN_RIGHT:
				strX = viewPos.x + (float)(cellSize-1) * (TILE_WIDTH/2);
				strY = viewPos.y - (float)(cellSize-1) * (TILE_HEIGHT/2);
				idx = 0;
				for(int i = 0; i < cellSize; i++){
					for(int j = 0; j < cellSize-i; j++){
						TextureRegion tr;
						if(j == cellSize-i-1)
							tr = Assets.TEX_REG_TERRAIN_TRIANGLE_DOWNRIGHT;
						else
							tr = Assets.TEX_REG_TERRAIN_RECTTILE;
						
						rectView[idx++].setXY_WH_TR(strX - j*(TILE_WIDTH), 
								strY + i*(TILE_WIDTH), TILE_WIDTH, TILE_HEIGHT, tr);
					}
				}
			break;
		}
	}
	
	@Override
	public void draw(SpriteBatcher batcher) {
		for(int i = 0; i < rectView.length; i++){
			batcher.drawSprite(rectView[i].x, rectView[i].y, rectView[i].w, rectView[i].h, rectView[i].tr); 
		}
	}

	@Override
	public float getPosX() {
		return viewPos.x;
	}

	@Override
	public float getPosY() {
		return viewPos.y;
	}

	@Override
	public void setPos(float x, float y) {
		
	}

	@Override
	public boolean getEnableBool() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void setEnable(boolean enalbeBool) {
		// TODO Auto-generated method stub
	}

	@Override
	public void setEnergy(int energy) {
		// TODO Auto-generated method stub
	}

	@Override
	public int getEnergy() {
		// TODO Auto-generated method stub
		return 0;
	}
}
